

<?php
	include_once("header.php");
	$valid = true;
	if(isset($_GET['id']) && isset($_GET['qfid'])){
		require('conn.php');
		$qfid = $_GET['qfid'];
		$questionQuery = mysql_query("select name from questionForm where qfid = $qfid");
		if($question = mysql_fetch_assoc($questionQuery)){
			echo "<div style='font-size:20px'><".$question['name']."></div><br>";
		}
		$qid = $_GET['id'];
		$typeQuery = mysql_query("select type from question where qid = $qid");
		if($type = mysql_fetch_assoc($typeQuery)){
			$index = 0;
			if($type['type'] == 0){
				if(!isset($_GET['page'])){
					$answerArray = mysql_query("select answer from open where qid = $qid");			
					if($answer = mysql_fetch_assoc($answerArray)){
						do{
							echo "A".$index.". ".$answer['answer']."<br>";
							$index++;
						}while($answer = mysql_fetch_assoc($answerArray));
					}
					else
						echo "No one has answered.";
				}
				else{
					$amount = 5;
					$page = $_GET['page'];
					$start = ($page-1)*$amount;
					$answerArray = mysql_query("select answer from open where qid = $qid limit $start,$amount");
					if($answer = mysql_fetch_assoc($answerArray)){
						do{
							echo "A".($start+$index).". ".$answer['answer']."<br>";
							$index++;
						}while($answer = mysql_fetch_assoc($answerArray));
						echo "<br>";
						$next = $page+1;
						$back = $page-1;
						$totalOpen = mysql_query("select * from open where qid = $qid");
						$max = mysql_num_rows($totalOpen);
						$last = ceil($max/$amount);
								
						echo "<a href='statistic.php?id=$qid&qfid=$qfid&page=1'><<</a>&nbsp;&nbsp;&nbsp;&nbsp;";
						if($page > 1)
							echo "<a href='statistic.php?id=$qid&qfid=$qfid&page=$back'><</a>&nbsp;&nbsp;&nbsp;&nbsp;";
						echo "<a href='question_list.php?id=$qfid'>main</a>&nbsp;&nbsp;&nbsp;&nbsp;";
						if($last > $page)
							echo "<a href='statistic.php?id=$qid&qfid=$qfid&page=$next'>></a>&nbsp;&nbsp;&nbsp;&nbsp;";
						
						echo "<a href='statistic.php?id=$qid&qfid=$qfid&page=$last'>>></a> ";
								
					}
					else{
						echo "No one has answered.<br><br>";
						echo "<a href='question_list.php?id=$qfid'>main</a>&nbsp;&nbsp;&nbsp;&nbsp;";
					}
				}
			}
			else{
				$totalMC = mysql_query("select sum(vote) as total from mc where qid = $qid");
				$total = mysql_fetch_assoc($totalMC);
				$max = $total['total'];
				echo "<div id='graph' data-total='$max'>";
				if($max > 0){
					echo "<img src='graph.php?id=$qid'></div>";					
				}
				else{
					echo "No one has voted.</div>";
				}
				echo "<br><br><a href='question_list.php?id=$qfid'>main</a>";
?>
				<script src="js/jquery-1.11.0.js"></script>
				<script>
				function update(){
					$.ajax({
						type: 'GET',
						url: 'graph.php',
						dataType: 'json',
						data:{
							id: <?php echo $qid?>,
							prev: $('#graph').data('total')
						},
						success: function(data){
				
							var base64Image=data.image;
							if(base64Image != ''){
								
								var imageSrc = 'data:image/png;base64,'+base64Image;
								if($('#graph').data('total') == 0){
									$('#graph').html("<img src='"+imageSrc+"'>");
									
								}
								else{
									$('#graph').children('img').eq(0).attr('src',imageSrc);
								}
								$('#graph').data('total',data.total);
							}
						}
					});
					
				}
				setInterval(update,10000);
				</script>
<?php 
			}		
		}
		else
			$valid = false;
	}
	else{
		$valid = false;
	}
	if(!$valid)
		echo "Invalid access!";
?>
